package com.borqsprocessmanager.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.Constant;
import com.borqsprocessmanager.R;
import com.borqsprocessmanager.info.ProcessStatsInfo;
import com.borqsprocessmanager.info.SectionInfo;
import com.borqsprocessmanager.util.Log;

public class SampleAdapter extends SectionAdapter implements OnClickListener {
    final String TAG = "SampleAdapter";

    public interface OnItemButtonClickListener {
        public void onItemButtonClick(View v, int posation);
    }

    private OnItemButtonClickListener mOnItemClickListener;

    private Context mContext;
    private ArrayList<ProcessStatsInfo> mList;
    private LayoutInflater mInflater;
    private PackageManager pm;

    public SampleAdapter(Context context, ArrayList<ProcessStatsInfo> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);
        pm = context.getPackageManager();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProcessStatsInfo info = mList.get(position);
        int type = info.type;
        Log.v(TAG, "getView_info.type=" + info.type);
        if (type < 5) {
            convertView = createProcessItem(convertView, info, position);
        } else {
            convertView = createWarnItem(convertView, info, position, type);
        }
        return convertView;

    }

    public void setOnItemClickListener(OnItemButtonClickListener listener) {
        mOnItemClickListener = listener;
    }

    private View createWarnItem(View convertView, ProcessStatsInfo info,
            int position, int Type) {
        WarnViewHolder holder;
        convertView = mInflater.inflate(R.layout.res_list_item_signal, null);
        holder = new WarnViewHolder();
        holder.icon = (ImageView) convertView
                .findViewById(R.id.res_item_signal_icon);
        holder.title = (TextView) convertView
                .findViewById(R.id.res_item_signal_title);
        holder.data = (TextView) convertView
                .findViewById(R.id.res_item_signal_data);

        if (Type == Constant.TYPE_SIGNAL_LOW) {
            holder.icon.setImageResource(R.drawable.signal);
        }
        return convertView;
    }

    private View createProcessItem(View convertView, ProcessStatsInfo info,
            int position) {
        ViewHolder holder;

        // if (convertView == null) {
        convertView = mInflater.inflate(R.layout.res_list_item, null);
        holder = new ViewHolder();
        holder.icon = (ImageView) convertView
                .findViewById(R.id.res_item_image_icon);
        holder.actionBtn = (ImageButton) convertView
                .findViewById(R.id.res_item_image_action);
        holder.actionBtn.setOnClickListener(this);
        holder.appName = (TextView) convertView
                .findViewById(R.id.res_item_text_name);
        holder.cpuType = (TextView) convertView
                .findViewById(R.id.res_item_text_cpu);
        holder.netType = (TextView) convertView
                .findViewById(R.id.res_item_text_net);

        // convertView.setTag(holder);
        // } else {
        // holder = (ViewHolder) convertView.getTag();
        // }
        try {
            holder.icon.setImageDrawable(pm
                    .getApplicationIcon(info.packageName));
        } catch (NameNotFoundException e) {
            holder.icon.setImageDrawable(App.mContext.getResources()
                    .getDrawable(R.drawable.android));
        }
        holder.appName.setText(info.appName);

        holder.actionBtn.setTag(position);
        if (info.type == 1) {
            holder.cpuType.setVisibility(View.GONE);
            holder.netType.setVisibility(View.VISIBLE);
            holder.netType.setText("net " + info.total / 1024 + "KB");
        } else if (info.type == 2) {
            holder.cpuType.setVisibility(View.VISIBLE);
            holder.netType.setVisibility(View.GONE);
            holder.cpuType.setText("Cpu " + info.ratio + "%");
        } else if (info.type == 3) {
            holder.cpuType.setVisibility(View.VISIBLE);
            holder.netType.setVisibility(View.VISIBLE);
            holder.cpuType.setText("Cpu " + info.ratio + "%");
            holder.netType.setText("net " + info.total / 1024 + "KB");
        }

        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        ImageButton actionBtn;
        TextView appName;
        TextView cpuType;
        TextView netType;
    }

    static class WarnViewHolder {
        ImageView icon;
        ImageButton actionBtn;
        TextView title;
        TextView data;
    }

    @Override
    public SectionInfo getSectionInfo(int index) {
        // TODO Auto-generated method stub
        return mList.get(index);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemButtonClick(v, (Integer) v.getTag());
        }
    }

    @Override
    public void remove(int posaction) {
        if (mList != null && mList.size() > 0) {
            mList.remove(posaction);
            Log.v(TAG, "mList===" + mList.size());
        }
    }

    @Override
    public String getHeadTitle(int posaction) {
        // TODO Auto-generated method stub
        return null;
    }
}
